package c15IO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 测试字节流输入输出类
 *
 * @author 炜sama
 */
public class 字节流测试 {

	public void testFis() throws IOException {
		InputStream is = new FileInputStream("id");
		byte[] b = new byte[100];
		System.err.println(is.available());
		System.err.println(is.read(b, 0, 100));
		System.err.println(is.read());
		System.err.println(is.available());
		System.err.println(is.markSupported());
		is.mark(10);
		System.err.println(is.skip(203));
//		is.reset();
		System.err.println(is.available());
		System.err.println(is.read());
		is.close();
	}

	public void testFos() throws IOException {
		OutputStream os = new FileOutputStream("os");
		for (int i = 0; i < 100; i++) {
			os.write(i);
		}
		os.flush();
		os.close();
	}

	public void copyFile(String res, String dest) {
		File fr = new File(res);
		File fd = new File(dest);
		InputStream is = null;
		OutputStream os = null;

		if (!fr.exists() || fd.exists()) {
			return;
		}

		try {
			is = new FileInputStream(fr);
		} catch (FileNotFoundException e) {
			//TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
		try {
			os = new FileOutputStream(fd);
		} catch (FileNotFoundException e1) {
			//TODO Auto-generated catch block
			e1.printStackTrace();
			return;
		}
		int read = 0;
		byte[] buf = new byte[1024];
		try {
			while ((read = is.read(buf)) != -1) {
				os.write(buf, 0, read);
			}
		} catch (IOException e) {
			//TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			is.close();
		} catch (IOException e) {
			//TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			os.close();
		} catch (IOException e) {
			//TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		字节流测试 t = new 字节流测试();
//		new Test().copyFile("id", "id2");
//		t.testFis();
		t.testFos();
	}

}
